﻿Imports System

Namespace Compiler.Semantic

    Public Class BindingTable(Of T As Class)
        Private m_symbolHash As IDictionary(Of Binder, T)

        Public Sub New(ByVal symbolHash As IDictionary(Of Binder, T))
            m_symbolHash = symbolHash
        End Sub

        Public Sub New()
            MyClass.New(New Dictionary(Of Binder, T))
        End Sub

        Public Function Enter(ByVal s As Binder, ByVal binding As T) As Boolean
            If m_symbolHash.ContainsKey(s) Then
                Return False
            Else
                m_symbolHash.Add(s, binding)
                Return True
            End If
        End Function

        Public Function TryLookUp(ByVal s As Binder, ByRef content As T) As Boolean
            If m_symbolHash.ContainsKey(s) Then
                content = m_symbolHash(s)
                Return True
            Else
                Return False
            End If
        End Function

        Public Function LookUp(ByVal s As Binder) As T
            If m_symbolHash.ContainsKey(s) Then
                Return m_symbolHash(s)
            Else
                Return Nothing
            End If
        End Function

        Public ReadOnly Property Count() As Integer
            Get
                Return m_symbolHash.Count
            End Get
        End Property

        Public Function ToList() As List(Of T)
            Return New List(Of T)(m_symbolHash.Values)
        End Function
    End Class

End Namespace